<template>
  <div class="mod-sp__spteam">
    <el-form :inline="true" :model="dataForm" @keyup.enter="getDataList()">
      <el-form-item>
        <el-input v-model="dataForm.teamName" placeholder="代表队名称" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-input v-model="dataForm.company" placeholder="单位" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-input v-model="dataForm.phone" placeholder="手机号" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-input v-model="dataForm.realName" placeholder="负责人姓名" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <ren-select v-model="dataForm.matchLevel" dict-type="matchLevel" placeholder="赛事等级"></ren-select>
      </el-form-item>
      <el-form-item>
        <ren-select v-model="dataForm.season" dict-type="season" placeholder="赛季年度"></ren-select>
      </el-form-item>
      <el-form-item label="地区" prop="province">
        <el-select style="width: 150px" placeholder="请选择" v-model="dataForm.province" @change="selectCity">
          <el-option value="" label="全国">全国</el-option>
          <el-option v-for="itemO in area.item" :key="itemO.code" :label="itemO.name" :value="itemO.code"> </el-option>
        </el-select>
        ~
        <el-select style="width: 150px" placeholder="请选择" v-model="dataForm.city">
          <el-option value="" label="全部">全部</el-option>
          <el-option v-for="itemO in areaCity.item" :key="itemO.code" :label="itemO.name" :value="itemO.code"> </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button @click="getDataList()">{{ $t("query") }}</el-button>
      </el-form-item>
      <el-form-item>
        <el-button type="info" @click="exportHandle()">{{ $t("export") }}</el-button>
      </el-form-item>
      <!--
      <el-form-item>
        <el-button v-if="hasPermission('sp:spteam:save')" type="primary" @click="addOrUpdateHandle()">{{ $t("add") }}</el-button>
      </el-form-item>
      <el-form-item>
        <el-button v-if="hasPermission('sp:spteam:delete')" type="danger" @click="deleteHandle()">{{ $t("deleteBatch") }}</el-button>
      </el-form-item>
      -->
    </el-form>
    <el-table v-loading="dataListLoading" :data="dataList" border @selection-change="dataListSelectionChangeHandle" style="width: 100%">
      <el-table-column type="selection" header-align="center" align="center" width="50"></el-table-column>
      <el-table-column prop="teamName" label="代表队名称" header-align="center" align="center" width="180">
        <template v-slot="scope">
          <el-button type="text" @click="showTypeList(scope.row)">{{ scope.row.teamName }}</el-button>
        </template>
      </el-table-column>
      <el-table-column prop="company" label="单位" header-align="center" align="center" width="230"></el-table-column>
      <el-table-column prop="provinceName" label="省" header-align="center" align="center" width="120"></el-table-column>
      <el-table-column prop="cityName" label="市" header-align="center" align="center" width="120"></el-table-column>
      <el-table-column prop="leader" label="负责人姓名" header-align="center" align="center" width="120">
        <template v-slot="scope">
          <el-button type="text" @click="detailHandle(scope.row.creator)">{{ scope.row.leader }}</el-button>
        </template>
      </el-table-column>
      <el-table-column prop="phone" label="手机号" header-align="center" align="center" width="120"></el-table-column>
      <el-table-column prop="teamCode" label="代表队编号" header-align="center" align="center" width="120"></el-table-column>
      <el-table-column prop="phone" label="状态" header-align="center" align="center" width="120">
        <template v-slot="scope">
          {{ scope.row.status == 1 ? "启用" : "禁用" }}
        </template>
      </el-table-column>
      <el-table-column prop="sex" label="性别" header-align="center" align="center" width="120">
        <template v-slot="scope">
          {{ getDictLabel("gender", scope.row.sex) }}
        </template>
      </el-table-column>
      <el-table-column prop="idType" label="证件类别" header-align="center" align="center" width="120">
        <template v-slot="scope">
          {{ getDictLabel("idType", scope.row.idType) }}
        </template>
      </el-table-column>
      <el-table-column prop="idCard" label="身份证号" header-align="center" align="center" width="140">
        <template v-slot="scope">
          {{ hideIdCard(scope.row.idCard) }}
        </template>
      </el-table-column>
      <el-table-column :label="$t('handle')" fixed="right" header-align="center" align="center" width="150">
        <template v-slot="scope">
          <el-button v-if="scope.row.status == '1'" type="text" size="small" @click="updStatusHandle(scope.row.id, '0')">禁用</el-button>
          <el-button v-if="scope.row.status == '0'" type="text" size="small" @click="updStatusHandle(scope.row.id, '1')">启用</el-button>
          <!--
          <el-button v-if="hasPermission('sp:spteam:update')" type="text" size="small" @click="addOrUpdateHandle(scope.row.id)">{{ $t("update") }}</el-button>
          <el-button v-if="hasPermission('sp:spteam:delete')" type="text" size="small" @click="deleteHandle(scope.row.id)">{{ $t("delete") }}</el-button>
          -->
        </template>
      </el-table-column>
    </el-table>
    <el-pagination :current-page="page" :page-sizes="[10, 20, 50, 100]" :page-size="limit" :total="total" layout="total, sizes, prev, pager, next, jumper" @size-change="pageSizeChangeHandle" @current-change="pageCurrentChangeHandle"> </el-pagination>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
    <!-- 字典类型数据 -->
    <el-drawer :title="focusDictTypeTitle" v-model="dictTypeListVisible" :size="800" close-on-press-escape="false" custom-class="rr-drawer">
      <DictTypeList :dictTypeId="focusDictTypeId"></DictTypeList>
    </el-drawer>
    <detail v-if="detailVisible" ref="send" @refreshDataList="getDataList"></detail>
  </div>
</template>

<script lang="ts">
import useView from "@/hooks/useView";
import { defineComponent, reactive, toRefs } from "vue";
import AddOrUpdate from "./spteam-add-or-update.vue";
import DictTypeList from "./spteamuser.vue";
import { IObject } from "@/types/interface";
import baseService from "@/service/baseService";
import Detail from "./spuser-detail.vue";
export default defineComponent({
  components: {
    AddOrUpdate,
    DictTypeList,
    Detail
  },
  setup() {
    const state = reactive({
      getDataListURL: "/sp/spteam/page",
      getDataListIsPage: true,
      exportURL: "/sp/spteam/export",
      deleteURL: "/sp/spteam",
      limit: 20,
      deleteIsBatch: true,
      dataForm: {
        teamName: "",
        province: "",
        city: "",
        company: "",
        matchLevel: "",
        season: "",
        phone: "",
        realName: ""
      },
      dictTypeListVisible: false,
      focusDictTypeId: "",
      focusDictTypeTitle: "",
      area: { code: "0", item: [{ code: "0", name: "请选择" }] },
      areaCity: { code: "0", item: [{ code: "0", name: "请选择" }] },
      detailVisible: false
    });
    return { ...useView(state), ...toRefs(state) };
  },
  watch: {},
  created() {
    this.areaList(this.area);
  },
  methods: {
    detailHandle(id?: string) {
      this.detailVisible = true;
      baseService.get("/sp/spuser/" + id).then((e: any) => {
        this.$nextTick(() => {
          if (e.data) {
            e.data.sex = e.data.sex == 0 ? "女" : "男";
          }
          this.$refs.send.dataForm.row = e.data;
          this.$refs.send.init();
        });
      });
    },
    // 获取省份
    areaList(target: any) {
      baseService.get("/front/fast/sys/areaDict/list?pid=" + target.code).then((e: any) => {
        target.item = e.data;
      });
    },
    // 选择城市
    selectCity() {
      if (this.dataForm.province) {
        this.areaCity.code = this.dataForm.province;
        this.areaList(this.areaCity);
      } else {
        this.dataForm.city = "";
        this.areaCity.item = [];
      }
    },
    showTypeList(row: IObject) {
      this.dictTypeListVisible = true;
      this.focusDictTypeId = row.id;
      this.focusDictTypeTitle = `领队成员列表`;
    },
    updStatusHandle(id: string, status: string) {
      this.$confirm("确认" + (status == "1" ? "启用" : "禁用") + "该领队?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        baseService.post(`/sp/spteam/updStatus`, { id: id, status: status }).then(() => {
          this.$message.success("操作成功");
          this.getDataList();
        });
      });
    },
    /**
     * 身份证隐藏中间十位
     */
    hideIdCard(idCard: string): string {
      //验证身份证号必须满足18位，最后一位可以是字母
      if (!/^\d{17}(\d|x)$/i.test(idCard)) {
        return idCard;
      } else {
        return idCard.replace(/^(.{4})(?:\d+)(.{2})$/, "$1****$2");
      }
    }
  }
});
</script>
